package com.xiaoge.zhengfeng.activitys;

import android.app.ActivityGroup;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
import android.widget.TextView;

import com.xiaoge.zhengfeng.R;

public class MainActivity extends ActivityGroup {

    private TabHost tabHost;//声明一个TabHost对象
    private ImageView msgImageView;
    //资源文件
    private Class activitys[] = {TabActivity1.class, TabActivity2.class,
            TabActivity3.class, TabActivity4.class};//跳转的Activity
    private String title[] = {"首页", "消息", "更多", "会员"};//设置菜单的标题
    private int image[] = {R.drawable.home_selector, R.drawable.speech_selector,
            R.drawable.more_selector, R.drawable.contact_selector,};//设置菜单

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        msgImageView = new ImageView(this);

        initTabView();// 初始化tab标签
    }

    private void initTabView() {
        // 实例化tabhost
        this.tabHost = (TabHost) findViewById(R.id.mytabhost);

        // 由于继承了ActivityGroup，所以需要在setup方法里加入此参数，若继承TabActivity则可省略
        tabHost.setup(this.getLocalActivityManager());

        // 去掉TabHost中间的竖线
        tabHost.getTabWidget().setDividerDrawable(null);

        // 创建标签
        for (int i = 0; i < activitys.length; i++) {
            //实例化一个view作为tab标签的布局
            View view = View.inflate(this, R.layout.tab_layout, null);

            // 设置imageview
            ImageView imageView = (ImageView) view.findViewById(R.id.image);
            imageView.setImageDrawable(getResources().getDrawable(image[i]));

            // 设置textview
            TextView textView = (TextView) view.findViewById(R.id.title);
            textView.setText(title[i]);

            // 设置消息小红点
            final ImageView redPoint = (ImageView) view.findViewById(R.id.red_point);

            if (i == 1) {
                redPoint.setVisibility(View.INVISIBLE);
                msgImageView = redPoint;
            }

            // 设置跳转activity
            Intent intent = new Intent(this, activitys[i]);

            // 载入view对象并设置跳转的activity
            TabSpec spec = tabHost.newTabSpec(title[i]).setIndicator(view).setContent(intent);

            tabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() {
                @Override
                public void onTabChanged(String tabId) {
                    if (tabId.equals(title[1])) {
                        // 选这个标签的时候 隐藏红点
                        msgImageView.setVisibility(View.INVISIBLE);
                    }


                }
            });

            // 添加到选项卡
            tabHost.addTab(spec);

        }
    }
}
